﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace BricksBaneEditor
{
    public partial class BrickGrid : UserControl
    {
        internal Wall Wall
        {
            get;
            set;
        }

        [Category("Appearance"), Description("Toggle visibility of 16x16 reference grid")]
        public bool DrawGrid
        {
            get;
            set;
        }

        [Category("Appearance"), Description("Toggle visibility of bricks' borders")]
        public bool DrawBricksBorders
        {
            get;
            set;
        }

        public BrickGrid()
        {
            InitializeComponent();

            this.DoubleBuffered = true;
        }

        private static readonly Pen[] PENS = {Pens.White, Pens.Red, Pens.Lime, Pens.Blue};

        public const int GRID_WIDTH = 8;
        public const int GRID_HEIGHT = 8;

        private void BrickGrid_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Clear(Color.Gray);

            if (DrawGrid)
            {
                Pen pen = Pens.Black;

                int x = 0, y = 0;
                while (y < Height)
                {
                    g.DrawLine(pen, 0, y, Width, y);
                    y += GRID_HEIGHT;
                }
                while (x < Width)
                {
                    g.DrawLine(pen, x, 0, x, Height);
                    x += GRID_WIDTH;
                }
            }

            if (Wall == null)
                return;           

            foreach (Brick brick in Wall.Bricks)
            {
                SolidBrush brush = new SolidBrush(brick.Color);
                g.FillRectangle(brush, brick.Rect);
                if (DrawBricksBorders)
                    g.DrawRectangle(PENS[(int)brick.Type], brick.Rect);
            }
        }

    }
}
